package edu_admin;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;


public class getContent {
    String[] href;
    BufferedReader br;
	
	public getContent(){
		Href_Title_Time htt=new Href_Title_Time();
		htt.getparts(htt.usefulcode);
		href=htt.href;
		
	}
	
	
	public String[] getContext(String[] hr){
		String[] usefulcode=new String[20];
		for(int i=0;i<20;i++){
			
			String allcode=null;
			try {
				URL url=new URL("http://jw.nju.edu.cn"+hr[i]);
				
				try {
					br = new BufferedReader(new InputStreamReader(url.openStream()));
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				String lines=null;
				try {
					while((lines=br.readLine())!=null){
						allcode+=lines;
					
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (MalformedURLException e) {
				System.out.print("get context error");	
				e.printStackTrace();
			}
			
			String[] n1=allcode.split("<!--##begin content##-->");
			String[] n2=n1[1].split("<!--##end content##-->");
			usefulcode[i]=n2[0];
			
			                                              //get code containing context;
			
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*td\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*tr\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*form\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*font\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*script\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*strong\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*span\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*table\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*div\\s*([^>]*)\\s*>","" );
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*em\\s*([^>]*)\\s*>","" );
			
			usefulcode[i]=usefulcode[i].replace("<a href=\"..","http://jw.nju.edu.cn");
			usefulcode[i]=usefulcode[i].replace("\" target=\"_blank\">","");
			
			usefulcode[i]=usefulcode[i].replace("</td>","");
			usefulcode[i]=usefulcode[i].replace("<br/>","");
			usefulcode[i]=usefulcode[i].replace("<td>","");
			usefulcode[i]=usefulcode[i].replace("</tr>","");
			usefulcode[i]=usefulcode[i].replace("<tr>","");
			usefulcode[i]=usefulcode[i].replace("</table>","");
			usefulcode[i]=usefulcode[i].replace("<table>","");
			usefulcode[i]=usefulcode[i].replace("</form>","");
			usefulcode[i]=usefulcode[i].replace("</font>","");
			usefulcode[i]=usefulcode[i].replace("</script>","");
			usefulcode[i]=usefulcode[i].replace("</strong>","");
			usefulcode[i]=usefulcode[i].replace("</span>","");
			usefulcode[i]=usefulcode[i].replace("</p>","");
			usefulcode[i]=usefulcode[i].replace("</a>","");
			usefulcode[i]=usefulcode[i].replace("</div>","");
			usefulcode[i]=usefulcode[i].replace("</em>","");
			usefulcode[i]=usefulcode[i].replace("<tbody>","");
			usefulcode[i]=usefulcode[i].replace("</tbody>","");
			usefulcode[i]=usefulcode[i].replace("&middot;","·");
			usefulcode[i]=usefulcode[i].replace("&amp;","&");
			usefulcode[i]=usefulcode[i].replace("[！!]","");
			usefulcode[i]=usefulcode[i].replace("function submit(){var  th=document.form1;th.action=\"down-file.do\";th.submit();}","");
			
			usefulcode[i]=usefulcode[i].replaceAll("<\\s*a\\s*([^>]*)\\s*>","" );
			
			
			
			                                             //tables to be deleted;
			
			 
			usefulcode[i]=usefulcode[i].replace("&nbsp;","");
			usefulcode[i]=usefulcode[i].replace("&ldquo;","\"");
			usefulcode[i]=usefulcode[i].replace("&rdquo;","\"");
			usefulcode[i]=usefulcode[i].replace("&mdash;","-");
			
			                                            //tables symboled line feed;
			}
		return usefulcode;
	}
			
			
		public void toSave(String[] doc){
			for(int i=0;i<doc.length;i++){
				try {
					BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(i+".txt")));
				    try {
					
						    bw.write(doc[i]);
						    bw.close();
				
					}
				
				    catch (IOException e) {
					    e.printStackTrace();
				    }
				
			    } catch (FileNotFoundException e) {
				    System.out.print("notice save error");
				    e.printStackTrace();
			    }
			
			
			
		    }
	}
		


}
